package cn.com.kblog.model;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.kblog.common.Constant;
import cn.com.kblog.common.Result;
import cn.com.kblog.util.CryptUtils;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * `id` int(11) NOT NULL AUTO_INCREMENT,
  `nick_name` varchar(50) DEFAULT NULL,
  `password` varchar(128) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  	email_verify
  `create_time` TIMESTAMP default CURRENT_TIMESTAMP,
  `last_login_time` TIMESTAMP,
 */
/**
 * @author KK.Luo
 * Email luokai8@vip.qq.com
 * @date 2013-12-1下午1:24:08
 */
@TableBind(tableName = "user_info",pkName = "id")
public class User extends Model<User>{

	private static final long serialVersionUID = 5040359782603346775L;
	public static final User dao  =  new User();
	
	public static final String NICK_NAME = "nick_name";
	public static final String EMAIL = "email";
	public static final String PASSWORD = "password";
	public static final String EMAIL_VERIFY = "email_verify";
	
	public static final Integer EMAIL_VERIFY_YES = 1;
	public static final Integer EMAIL_VERIFY_NO = 0;
	
	/**
	 * 根据参数分页查询User
	 * @param cpage
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param allParams
	 * @return
	 */
	public Page<User> pageQuery(int cpage, int pageSize, String orderField,
		String orderDirection, Map<String, String> allParams) {
		String sql_select = "select * ";
		StringBuffer conf = new StringBuffer(" from user_info t where 1=1 ");
		List<String> params = new ArrayList<String>();
		if (allParams == null) {
		    allParams = new HashMap<String, String>();
		}
		if (StringKit.notBlank(allParams.get("nick_name"))) {
		    conf.append(" and lower(t.nick_name) like ? ");
		    params.add("%"+allParams.get("nick_name")+"%");
		}
		if (StringKit.notBlank(allParams.get("email"))) {
		    conf.append(" and lower(t.email) like ? ");
		    params.add("%"+allParams.get("email")+"%");
		}
		
		if (StringKit.notBlank(orderField, orderDirection)) {
		    conf.append(" order by ").append(" t.").append(orderField).append(
			    " ").append(orderDirection);
		}
		return dao.paginate(cpage, pageSize, sql_select, conf.toString(),params.toArray());
	 }
	/**
	 * 保存或者更新
	 * @return
	 */
	public Result saveOrUpdate(){
		Result result = new Result();
		//对Password加密
		if(getStr("password")!=null)
			this.set("password", CryptUtils.encrypt(getStr("password"), Constant.CRYPT_KEY));
		//保存
		if (getID() == null) {
		   if(this.save()){
			    result.success("添加成功！"); 
		   }else{
			    result.error("添加失败！");
		   }
		} else {
		//更新
		   if(update()){
			   result.success("修改成功！"); 
		   }else{
			   result.error("修改失败！");
		   }
		}
		return result;
	}
	
	 
    /**
     * 删除用户
     * @param id
     * @return
     */
    public Result deleteById(String id) {
    	Result result = new Result();
    	
		if (StringKit.isBlank(id)) {
		     result.success("信息错误！");
		}
		 if(super.deleteById(id)){
			  result.success("删除成功！"); 
		 }else{
			  result.error("删除失败！");
		 }
		return result;
    }
    
	/**
	 * 更新最后的登陆时间
	 * @return
	 */
	public boolean updateLastLoginTime(){
		//更新时间
		this.set("last_login_time", new java.sql.Timestamp(Calendar.getInstance()
				.getTimeInMillis()));
		return update();
				
	}
	public Integer getID() {
		return getInt("id");
	}
	/**
	 * 根据用户名和密码查找用户
	 * @param username
	 * @param password
	 * @return
	 */
	public User login(String email, String password) {
		String sql = "select * from user_info where email=? and password=? and email_verify=1";
		return findFirst(sql,email,password);
	} 
	
	/**
	 * 根据邮箱查找email(包含验证和未验证)
	 * @param email
	 * @return
	 */
	public User findByEmail(String email) {
		String sql = "select * from user_info where email=?";
		return findFirst(sql,email);
	}
	/**
	 * 根据邮箱查找email(验证)
	 * @param email
	 * @return
	 */
	public User findByEmail2Verify(String email) {
		String sql = "select * from user_info where email=? and email_verify=1";
		return findFirst(sql,email);
	} 
}
